package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.ReportDao;
import com.itheima.entity.Result;
import com.itheima.service.ReportService;
import com.itheima.untils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportDao reportDao;
    @Override
    public List<Integer> getMemberReport(List<String> list) {
        List<Integer> timeList = new ArrayList<>();
        for (String str : list) {
            //根据时间查询每月会员人数
            String time = str+".31";
            Integer count = reportDao.findByTime(time);
            timeList.add(count);
        }
        return timeList;
    }

    @Override
    public Result getSetmealReport() {
        try {
            Map<String,Object> map = new HashMap<>();
            //先获取数据系列setmealCount
            List<Map<String,Object>> setmealCount = reportDao.findsetmealCount();
            //获取图例名称setmealNames
            List<String> setmealNames = new ArrayList<>();
            for (Map<String, Object> str : setmealCount) {
                String name = (String) str.get("name");
                setmealNames.add(name);
            }
            //把数据封装到map中
            map.put("setmealCount",setmealCount);
            map.put("setmealNames",setmealNames);
            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    @Override
    public Result getBusinessReportData() throws Exception{
        try {
            Map<String,Object> map = new HashMap<>();
            //获取当天时间
            String today = DateUtils.parseDate2String(new Date());
            //获取本周一日期
            String mondy = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
            //获取本月第一天
            String month = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
            //设置当天时间
            map.put("reportDate", today);
            //设置当天新增会员数
            Integer todayNewMember = reportDao.findTodayNewMember(today);
            map.put("todayNewMember",todayNewMember);
            //设置总会员数
            Integer totalMember = reportDao.findTotalMember();
            map.put("totalMember",totalMember);
            //设置这周新增会员数
            Integer thisWeekNewMember = reportDao.findThisWeekNewMember(mondy);
            map.put("thisWeekNewMember",thisWeekNewMember);
            //设置这月新增会员数
            Integer thisMonthNewMember=reportDao.findThisWeekNewMember(month);
            map.put("thisMonthNewMember",thisMonthNewMember);


            //设置今日预约数
            Integer todayOrderNumber = reportDao.findTodayOrderNumber(today);
            map.put("todayOrderNumber",todayOrderNumber);
            //设置今日已经到诊数
            Integer todayVisitsNumber = reportDao.findTodayVisitsNumber(today);
            map.put("todayVisitsNumber",todayVisitsNumber);
            //设置这周预约数
            Integer thisWeekOrderNumber = reportDao.findThisWeekOrderNumber(mondy);
            map.put("thisWeekOrderNumber",thisWeekOrderNumber);
            //设置这周已到诊人数
            Integer thisWeekVisitsNumber = reportDao.findThisWeekVisitsNumber(mondy);
            map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
            //设置这月预约人数
            Integer thisMonthOrderNumber = reportDao.findThisWeekOrderNumber(month);
            map.put("thisMonthOrderNumber",thisMonthOrderNumber);
            //设置这月已到诊人数
            Integer thisMonthVisitsNumber = reportDao.findThisWeekVisitsNumber(month);
            map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);


            List<Map<String,Object>> hotSetmeal = reportDao.findHotSetmeal();
            map.put("hotSetmeal",hotSetmeal);
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }

    @Override
    public Result getOrderStatusReportData(String beginDate, String endDate) {
        try {
            if (beginDate.equals("")||beginDate.length()==0){
                beginDate = null;
            }
            if (endDate.equals("")||endDate.length()==0){
                endDate = null;
            }
            //查询所有有预约的时间
            List<Date> dates = reportDao.getOrder(beginDate,endDate);
            List<String> dateList = new ArrayList<>();
            List<Integer> orderCount = new ArrayList<>();
            List<Integer> statusList = new ArrayList<>();
            for (Date date : dates) {
                //根据时间查询每天预约人数
                Integer count = reportDao.getReport(date);
                orderCount.add(count);
                //查询已预约人数
                Integer status = reportDao.findStatus(date);
                statusList.add(status);
                dateList.add(DateUtils.parseDate2String(date));
            }
            Map<String,Object> map = new HashMap<>();
            map.put("time",dateList);
            map.put("orderCounts",orderCount);
            map.put("visitsCounts",statusList);
            return new Result(true,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    @Override
    public Result getSetmealMoney(String beginDate, String endDate) {
        try {
            if (beginDate.equals("")||beginDate.length()==0){
                beginDate = null;
            }
            if (endDate.equals("")||endDate.length()==0){
                endDate = null;
            }
            //查询所有套餐和对应的id的数量
            List<Integer> integers = reportDao.findSetmealId(beginDate,endDate);
            //list  有序
            List<String> setmealName = new ArrayList<>();
            //名称对应收入
            List<Map<String,Object>> mapList = new ArrayList<>();
            Integer sum = 0;
            for (Integer id : integers) {
                //根据id查询套餐的数量
                Integer count = reportDao.findCount(id);
                //根据id查询套餐名称和钱数
                String setmealname = reportDao.findNameById(id);
                String price = reportDao.findPriceById(id);
                //该套餐的收入
                Integer pri = Integer.parseInt(price)*count;
                //添加套餐名称
                setmealName.add(setmealname);
                Map<String,Object> al = new HashMap<>();
                al.put("name", setmealname);
                al.put("value",pri);
                mapList.add(al);
                sum +=pri;
            }
            setmealName.add("总收入");
            //添加总收入
            Map<String,Object> aa =new HashMap<>();
            aa.put("name","总收入");
            aa.put("value",sum);
            mapList.add(aa);
            //把查询出来的所有数据封装到map返回
            Map<String,Object> hashMap = new HashMap<>();
            hashMap.put("setmeal",setmealName);
            hashMap.put("setmealmoney",mapList);
            return new Result(true,"查询成功",hashMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    @Override
    public Result findSex() {
        try {
            //查询数据库获取性别男
            Integer sexNan = reportDao.findSex(1);
            //查询数据库获取性别女
            Integer sexNv =  reportDao.findSexNv(2);
            Map<String,Object> map = new HashMap<>();
            List<String> stringList = new ArrayList<>();
            stringList.add("男");
            stringList.add("女");
            List<Integer> sexList = new ArrayList<>();
            sexList.add(sexNan);
            sexList.add(sexNv);
            //添加到map
            map.put("sexName",stringList);
            map.put("sexCount",sexList);
            return new Result(true,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
}
